package com.sample.notifications;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class NotificationsActivity extends Activity implements OnClickListener{
    
	private static final int NOTIFY_ME_ID = 1337;
	private Timer timer = new Timer();
	private int count = 0;
	Button b1, b2;
	
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
    }

	public void onClick(View v) 
	{ 
		if(v.equals(b1))
		{
			TimerTask task = new TimerTask()
			{
				public void run()
				{
					notifyMe();
				}
			};
			timer.schedule(task, 0, 5000);
		}
		else if(v.equals(b2))
		{
			NotificationManager mgr = (NotificationManager)(getSystemService(NOTIFICATION_SERVICE));
			mgr.cancel(NOTIFY_ME_ID);
			timer.cancel();
		}
		
	}
	void notifyMe()
	{
		final NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		Notification note = new Notification(R.drawable.ic_launcher, "WASSUP?!?", System.currentTimeMillis());
		PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, NotificationsActivity.class), 0);
		note.setLatestEventInfo(this, "TITLE", "MESSAGE!", i);
		note.number = ++count;
		note.defaults |= Notification.DEFAULT_SOUND;
		note.defaults |= Notification.DEFAULT_VIBRATE;
		mgr.notify(NOTIFY_ME_ID, note);
	}
}